package cn.cqone.crm.controller;

import cn.cqone.crm.domain.Customer;
import cn.cqone.crm.domain.DictoryDetails;
import cn.cqone.crm.domain.Employee;
import cn.cqone.crm.domain.Tenant;
import cn.cqone.crm.query.CustomerQuery;
import cn.cqone.crm.service.CustomerService;
import cn.cqone.crm.service.EmployeeService;
import cn.cqone.crm.service.TenantService;
import cn.cqone.crm.utils.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/customer")
@CrossOrigin
@RequiresPermissions("customer:main")
public class CustomerController extends BaseController<Customer,Long,CustomerQuery> {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private TenantService tenantService;

    //联表新增
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public JSONResult insert(@RequestBody Customer customer){
        try{
            Long id = customer.getInputUser_id().getId();
            Employee e = employeeService.findById(id);
            Tenant tenant = e.getTenant();
            customer.setTenant_id(tenant);
            customerService.add(customer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }

    //联表修改
    @RequestMapping(value = "/updateAll",method = RequestMethod.POST)
    public JSONResult updateAll(@RequestBody Customer customer){
        try{
            customerService.update(customer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("修改失败 请仔细检查");
        }
    }

    //移到资源池
    @RequestMapping(value = "/moveToPool",method = RequestMethod.POST)
    public JSONResult moveToPool(@RequestBody Customer customer){
        try{
            customerService.moveToPool(customer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("移动失败！");
        }
    }

    //移出资源池
    @RequestMapping(value = "/moveOutPool",method = RequestMethod.POST)
    public JSONResult moveOutPool(@RequestBody Customer customer){
        try{
            customerService.moveOutPool(customer);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("移动失败！");
        }
    }

    //职业下拉框
    @ResponseBody
    @RequestMapping(value = "/selectJobs",method = RequestMethod.POST)
    public JSONResult selectJobs(){
        try{
            List<DictoryDetails> list = customerService.findJobs();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }
    //收入水平下拉框
    @ResponseBody
    @RequestMapping(value = "/selectSalary",method = RequestMethod.POST)
    public JSONResult selectSalary(){
        try{
            List<DictoryDetails> list = customerService.findSalary();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //客户来源下拉框
    @ResponseBody
    @RequestMapping(value = "/selectCusSource",method = RequestMethod.POST)
    public JSONResult selectCusSource(){
        try{
            List<DictoryDetails> list = customerService.findCusSource();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //市场专员下拉框
    @ResponseBody
    @RequestMapping(value = "/selectSeller",method = RequestMethod.POST)
    public JSONResult selectSeller(){
        try{
            List<Employee> list = employeeService.findAll();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //客户来源下拉框
    @ResponseBody
    @RequestMapping(value = "/selectMoveType",method = RequestMethod.POST)
    public JSONResult selectMoveType(){
        try{
            List<DictoryDetails> list = customerService.findMoveType();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

}
